package test.poi;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFShape;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class ExcelImageExtractor {

    public static void main(String[] args) {
        String excelFilePath = "d:/poi/ddd2.xlsx";
        String outputFolder = "d:/poi/";

        try {
            // 创建工作簿
            Workbook workbook = WorkbookFactory.create(new FileInputStream(excelFilePath));

            // 获取所有Sheet
            for (Sheet sheet : workbook) {
                // 遍历Sheet中的所有图片
                Workbook wb = sheet.getWorkbook();
                for (PictureData pictureData : wb.getAllPictures()) {
                    // 生成图片文件名
                    String fileName = outputFolder + "aaa.png";

                    // 将图片数据保存为文件
                    FileOutputStream outputStream = new FileOutputStream(fileName);
                    outputStream.write(pictureData.getData());
                    outputStream.close();

                    System.out.println("提取图片成功：" + fileName);
                }
            }

            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}